﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Time.Model;

namespace Time.App
{
    public partial class ManageModuleWindow : Form
    {
        private TimeDataContext db = new TimeDataContext();

        public ManageModuleWindow(Form owner)
        {
            this.Owner = owner;
            InitializeComponent();

            List<Module> modules = db.Modules.Where(m => m.Parent == null).ToList();

            foreach (Module module in modules)
            {
                TreeNode root = treeView1.Nodes.Add(module.Name);
                root.Tag = module;
                PopulateTreeView(root, module);
            }
        }

        private void PopulateTreeView(TreeNode rootNode, Module module)
        {
            foreach (Module submodules in module.Modules)
            {
                TreeNode current = rootNode.Nodes.Add(submodules.Name);

                if (submodules.Modules.Count > 0)
                    PopulateTreeView(current, submodules);
            }
        }
    }
}
